/*
    Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "LogoScene.h"

static std::string ACTION_SHPE_NAME = "{4B579CA4-F020-4EA2-8836-83B42A047403}";

LogoScene::LogoScene() : m_bg(nullptr), m_logoShpe(nullptr)
{
}

LogoScene::~LogoScene()
{
}

void LogoScene::enterScene()
{
    auto cache = GameWorld::getInstance()->getDefultSpriteFrameCache();
    m_bg = Sprite::create(cache->getSpriteFrame(L"logo_bg"), this);
    m_logoShpe = Sprite::create(cache->getSpriteFrame(L"shpe"), this);
    m_logoText = Sprite::create(cache->getSpriteFrame(L"text"), this);

    auto size = GameWorld::getInstance()->getAppView()->getWinSize();
    m_bg->setPosition(Vector2(size.width / 2, size.height / 2));
    m_logoShpe->setPosition(Vector2(size.width, size.height / 2 + 100));
    m_logoText->setPosition(Vector2(-200, size.height / 2 - 50));

    auto pos = Vector2(size.width / 2, size.height / 2 + 100);
    auto moveActionShep = ActionMove::createMoveToTargetPos(pos.x, pos.y, 1);
    m_logoShpe->runAction(moveActionShep->convertToActionSharedPtr());

    auto rotateActionShep = ActionSimple::create([](IActableObject *target, float ratio)
                                                 {
                                                     auto s = dynamic_cast<Sprite *>(target);
                                                     if (!s)
                                                     {
                                                         return;
                                                     }

                                                     s->setRotationA(720 * ratio); },
                                                 1);
    m_logoShpe->runAction(rotateActionShep->convertToActionSharedPtr());

    GameWorld::getInstance()->getScheduler()->addSchedulerTaskDelay([this, size]()
                                                                    {
                                                                        auto moveActionText = ActionSimple::create([size](IActableObject *target, float ratio)
                                                                                                                   {
                                                                                                                       static float distance_text_move = (size.width / 2 + 100) / 0.7f;

                                                                                                                       auto s = dynamic_cast<Sprite *>(target);
                                                                                                                       if (!s)
                                                                                                                       {
                                                                                                                           return;
                                                                                                                       }

                                                                                                                       if (ratio <= 0.7)
                                                                                                                       {
                                                                                                                           s->setPosition(Vector2(distance_text_move * ratio, size.height / 2 - 50));
                                                                                                                       }
                                                                                                                       else
                                                                                                                       {
                                                                                                                           s->setPosition(Vector2((distance_text_move * 0.7) - ((100 / 0.3f) * (ratio - 0.7)), size.height / 2 - 50));
                                                                                                                       }
                                                                                                                   },
                                                                                                                   0.7);
                                                                        m_logoText->runAction(moveActionText->convertToActionSharedPtr()); },
                                                                    "{AC3B8A67-702C-4800-B325-BFE4B1ED9CC4}", 1000);

    return;
}

void LogoScene::quitScene()
{
}
